/**
 * Copyright (C) 2007 The AsyncMail Group <asyncmail@googlegroups.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.asyncmail.smtpserver.mina.handler;

import org.apache.mina.common.TrafficMask;
import org.apache.mina.filter.SSLFilter;
import org.springframework.beans.factory.annotation.Required;

import com.googlecode.asyncmail.common.DSNStatus;
import com.googlecode.asyncmail.smtp.SMTPRequest;
import com.googlecode.asyncmail.smtp.SMTPResponse;
import com.googlecode.asyncmail.smtp.SMTPResponseImpl;
import com.googlecode.asyncmail.smtp.SMTPRetCode;
import com.googlecode.asyncmail.smtpserver.SMTPSession;
import com.googlecode.asyncmail.smtpserver.core.CommandHandler;
import com.googlecode.asyncmail.smtpserver.handler.esmtp.EhloExtension;
import com.googlecode.asyncmail.smtpserver.mina.SMTPSessionImpl;

import javax.net.ssl.SSLContext;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class StartTLSCmdHandler implements CommandHandler, EhloExtension {

    private SSLContext sslContext;

    /**
     * @see com.googlecode.asyncmail.smtpserver.core.CommandHandler#getImplCommands()
     */
    public Collection<String> getImplCommands() {
        Collection<String> commands = new LinkedList<String>();
        commands.add("STARTTLS");
        return commands;

    }

    /**
     * @see com.googlecode.asyncmail.smtpserver.core.CommandHandler#onCommand(com.googlecode.asyncmail.smtpserver.SMTPSession,
     *      com.googlecode.asyncmail.smtp.SMTPRequestImpl)
     */
    public SMTPResponse onCommand(SMTPSession session, SMTPRequest command) {
        if (session instanceof SMTPSessionImpl) {
            ((SMTPSessionImpl) session).getIoSession().setTrafficMask(
                    TrafficMask.WRITE);
            session.writeSMTPResponse(new SMTPResponseImpl(
                    SMTPRetCode.SERVICE_READY, DSNStatus.getStatus(
                            DSNStatus.SUCCESS, DSNStatus.DELIVERY_VERSION)
                            + " Go ahead"));
            SSLFilter filter = new SSLFilter(sslContext);
            session.resetState();
            ((SMTPSessionImpl) session).getIoSession().getFilterChain()
                    .addFirst("sslFilter", filter);
            ((SMTPSessionImpl) session).getIoSession().setTrafficMask(
                    TrafficMask.ALL);
            return null;
        } else {
            // TODO return an error
            return new SMTPResponseImpl(SMTPRetCode.LOCAL_ERROR,
                    "DEFINE ERROR TEXT");
        }
    }

    /**
     * @see com.googlecode.asyncmail.smtpserver.handler.esmtp.EhloExtension#getImplementedEsmtpFeatures(com.googlecode.asyncmail.smtpserver.SMTPSession)
     */
    public List<String> getImplementedEsmtpFeatures(SMTPSession session) {
        if (session instanceof SMTPSessionImpl) {
            List<String> feat = new LinkedList<String>();
            feat.add("STARTTLS");
            return feat;
        } else {
            // TODO log something about incompatible session?
            return null;
        }
    }

    public SSLContext getSslContext() {
        return sslContext;
    }

    @Required
    public void setSslContext(SSLContext sslContext) {
        this.sslContext = sslContext;
    }

}